package com.hilaver.dzmis.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtFilsType;
import com.hilaver.dzmis.codetable.CtDyeing;
import com.hilaver.dzmis.codetable.CtColorFamily;
import com.hilaver.dzmis.codetable.CtColorType;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtSendBy;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.dao.BiDZColorDAO;
import com.hilaver.dzmis.order.OrderColorTest;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiDZColorServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	private CtServiceImpl ctService;
	
	private BiDZColorDAO dao;
	
	public BiDZColorServiceImpl(){
		this.dao = new BiDZColorDAO();
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiDZColor.class.getName(), id);
		return super.delete(BiDZColor.class.getName(), id);
	}

	public String edit(BiDZColor biDZColor) throws Exception {
		String operation = null;
		if(biDZColor.getId() != null){
			BiDZColor old = (BiDZColor)this.dao.get(BiDZColor.class.getName(), biDZColor.getId());
			if(biDZColor.getPhoto1Name() == null){
				biDZColor.setPhoto1(old.getPhoto1());
				biDZColor.setPhoto1Name(old.getPhoto1Name());
			}
			if(biDZColor.getPhoto2Name() == null){
				biDZColor.setPhoto2(old.getPhoto2());
				biDZColor.setPhoto2Name(old.getPhoto2Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			if(biDZColor.getOldId() != null){
				BiDZColor old = (BiDZColor)this.dao.get(BiDZColor.class.getName(), biDZColor.getOldId());
				if(biDZColor.getPhoto1Name() == null){
					biDZColor.setPhoto1(old.getPhoto1());
					biDZColor.setPhoto1Name(old.getPhoto1Name());
				}
				if(biDZColor.getPhoto2Name() == null){
					biDZColor.setPhoto2(old.getPhoto2());
					biDZColor.setPhoto2Name(old.getPhoto2Name());
				}
				this.dao.getSession().evict(old);
			}
			operation = "01";
		}
		
		this.dao.saveOrUpdate(biDZColor);
		super.editSysMbox(biDZColor, operation);
		return "{success: true}";
	}
	
	@Override
	public String get(int id) throws Exception {
		BiDZColor biDZColor = (BiDZColor)this.dao.get(BiDZColor.class.getName(), id);
		
		writeFile(realFileDir + biDZColor.getPhoto1Name(), biDZColor.getPhoto1());
		writeFile(realFileDir + biDZColor.getPhoto2Name(), biDZColor.getPhoto2());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<customerId>");
		xmlSB.append(biDZColor.getBiFranceCustomer().getId());
		xmlSB.append("</customerId>");
		
		xmlSB.append("<customerName>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiFranceCustomer().getName());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerName>");
		
		xmlSB.append("<customerDepartment>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiFranceCustomer().getDepartment()==null?"":biDZColor.getBiFranceCustomer().getDepartment());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerDepartment>");
		
		xmlSB.append("<yarnId>");
		xmlSB.append(biDZColor.getBiYarn().getId());
		xmlSB.append("</yarnId>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), biDZColor.getBiYarn().getCtYarnMeasure(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biDZColor.getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		xmlSB.append("<ctYarnMaterial1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial1()) && biDZColor.getBiYarn().getComposition1() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition1() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial1>");
		
		xmlSB.append("<ctYarnMaterial2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial2(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial2()) && biDZColor.getBiYarn().getComposition2() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition2() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial2>");
		
		xmlSB.append("<ctYarnMaterial3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial3(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial3()) && biDZColor.getBiYarn().getComposition3() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition3() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial3>");
		
		xmlSB.append("<ctYarnMaterial4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial4(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial4()) && biDZColor.getBiYarn().getComposition4() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition4() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial4>");
		
		xmlSB.append("<ctYarnMaterial5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), biDZColor.getBiYarn().getCtYarnMaterial5(), locale));
		if(!StringUtils.isEmpty(biDZColor.getBiYarn().getCtYarnMaterial5()) && biDZColor.getBiYarn().getComposition5() != null){
			xmlSB.append("(" + biDZColor.getBiYarn().getComposition5() + "%)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial5>");
		
		xmlSB.append("<factoryNameCN>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getBiYarn().getBiFactory() == null){
			xmlSB.append("");
		}else{
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameCN());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</factoryNameCN>");
		
		xmlSB.append("<factoryNameEN>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getBiYarn().getBiFactory() == null){
			xmlSB.append("");
		}else{
			xmlSB.append(biDZColor.getBiYarn().getBiFactory().getNameEN()==null?"":biDZColor.getBiYarn().getBiFactory().getNameEN());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</factoryNameEN>");
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biDZColor.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biDZColor.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biDZColor.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(getIsColorApprovedPhoto(biDZColor));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		if(biDZColor.getBiDZColor() != null){
			xmlSB.append("<oldId>");
			xmlSB.append(biDZColor.getBiDZColor().getId());
			xmlSB.append("</oldId>");
		}
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biDZColor));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}

	
	public String getWithDescription(int id) throws Exception{
		BiDZColor biDZColor = (BiDZColor)this.dao.get(BiDZColor.class.getName(), id);
		
		writeFile(realFileDir + biDZColor.getPhoto1Name(), biDZColor.getPhoto1());
		writeFile(realFileDir + biDZColor.getPhoto2Name(), biDZColor.getPhoto2());
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(this.toXMLString(biDZColor));
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biDZColor));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}
	
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiDZColor> objList = this.dao.getAllPagination(BiDZColor.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiDZColor obj : objList) {
			writeFile(realFileDir + obj.getPhoto1Name(), obj.getPhoto1());
			writeFile(realFileDir + obj.getPhoto2Name(), obj.getPhoto2());
			
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLString(obj));
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiDZColor.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLString(BiDZColor obj) throws Exception{
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append("<factoryNameCN>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(obj.getBiYarn().getBiFactory() == null){
			xmlSB.append("");
		}else{
			xmlSB.append(obj.getBiYarn().getBiFactory().getNameCN());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</factoryNameCN>");
		
		xmlSB.append("<factoryNameEN>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(obj.getBiYarn().getBiFactory() == null){
			xmlSB.append("");
		}else{
			xmlSB.append(obj.getBiYarn().getBiFactory().getNameEN()==null?"":obj.getBiYarn().getBiFactory().getNameEN());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</factoryNameEN>");
		
		xmlSB.append("<yarnReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getBiYarn().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnReference>");
		
		xmlSB.append("<ctYarnMeasure>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), obj.getBiYarn().getCtYarnMeasure(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMeasure>");
		
		xmlSB.append("<yarnAccount>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getBiYarn().getYarnAccount()==null?"":obj.getBiYarn().getYarnAccount());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</yarnAccount>");
		
		xmlSB.append("<ctYarnMaterial1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getBiYarn().getCtYarnMaterial1(), locale));
		if(!StringUtils.isEmpty(obj.getBiYarn().getCtYarnMaterial1()) && obj.getBiYarn().getComposition1() != null){
			xmlSB.append("&nbsp;(&nbsp;" + obj.getBiYarn().getComposition1() + "%&nbsp;)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial1>");
		
		xmlSB.append("<ctYarnMaterial2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getBiYarn().getCtYarnMaterial2(), locale));
		if(!StringUtils.isEmpty(obj.getBiYarn().getCtYarnMaterial2()) && obj.getBiYarn().getComposition2() != null){
			xmlSB.append("&nbsp;(&nbsp;" + obj.getBiYarn().getComposition2() + "%&nbsp;)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial2>");
		
		xmlSB.append("<ctYarnMaterial3>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getBiYarn().getCtYarnMaterial3(), locale));
		if(!StringUtils.isEmpty(obj.getBiYarn().getCtYarnMaterial3()) && obj.getBiYarn().getComposition3() != null){
			xmlSB.append("&nbsp;(&nbsp;" + obj.getBiYarn().getComposition3() + "%&nbsp;)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial3>");
		
		xmlSB.append("<ctYarnMaterial4>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getBiYarn().getCtYarnMaterial4(), locale));
		if(!StringUtils.isEmpty(obj.getBiYarn().getCtYarnMaterial4()) && obj.getBiYarn().getComposition4() != null){
			xmlSB.append("&nbsp;(&nbsp;" + obj.getBiYarn().getComposition4() + "%&nbsp;)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial4>");
		
		xmlSB.append("<ctYarnMaterial5>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getBiYarn().getCtYarnMaterial5(), locale));
		if(!StringUtils.isEmpty(obj.getBiYarn().getCtYarnMaterial5()) && obj.getBiYarn().getComposition5() != null){
			xmlSB.append("&nbsp;(&nbsp;" + obj.getBiYarn().getComposition5() + "%&nbsp;)");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnMaterial5>");
		
		xmlSB.append("<ctDyeing>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtDyeing.class.getName(), obj.getCtDyeing(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctDyeing>");
		
		xmlSB.append("<ctFilsType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtFilsType.class.getName(), obj.getCtFilsType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctFilsType>");
		
		xmlSB.append("<ctColorFamily>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtColorFamily.class.getName(), obj.getCtColorFamily(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctColorFamily>");
		
		xmlSB.append("<ctColorType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtColorType.class.getName(), obj.getCtColorType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctColorType>");
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), obj.getCtSeasonNumber(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSeasonNumber>");
		
		xmlSB.append("<ctSendBy1>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSendBy.class.getName(), obj.getCtSendBy1(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSendBy1>");
		
		xmlSB.append("<ctSendBy2>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSendBy.class.getName(), obj.getCtSendBy2(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSendBy2>");
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(obj.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(obj.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(obj.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(obj.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(getIsColorApprovedPhoto(obj));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		if(obj.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
			
			xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFranceCustomer().getDepartment()==null?"":obj.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		}
		
		xmlSB.append("<isApproved>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(getIsColorApproved(obj));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isApproved>");
		return xmlSB.toString();
	}
	
	public String getMaxReferenceIndex(String ctYarnMaterial, String ctColorFamily, String ctFilsType) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex(ctYarnMaterial, ctColorFamily, ctFilsType);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getIsColorApproved(BiDZColor color){
		String rtn = "02";
		Set orderColorTests = color.getOrderColorTests();
		OrderColorTest oct = null;
		for (Iterator iterator = orderColorTests.iterator(); iterator.hasNext();) {
			oct = (OrderColorTest) iterator.next();
			break;
		}
		if(oct != null){
			rtn = oct.getIsColorApproved();
		}else{
			if(color.getBiDZColor() != null){
				rtn = getIsColorApproved(color.getBiDZColor());
			}
		}
		return rtn;
	}
	
	public OrderColorTest getColorText(BiDZColor color){
		OrderColorTest oct = null;
		Set orderColorTests = color.getOrderColorTests();
		
		for (Iterator iterator = orderColorTests.iterator(); iterator.hasNext();) {
			oct = (OrderColorTest) iterator.next();
			break;
		}
		if(oct != null){
			return oct;
		}else{
			if(color.getBiDZColor() != null){
				oct = getColorText(color.getBiDZColor());
			}
		}
		return oct;
	}
	
	public String getIsColorApprovedPhoto(BiDZColor color){
		String rtn = "noimage.gif";
		Set orderColorTests = color.getOrderColorTests();
		OrderColorTest oct = null;
		for (Iterator iterator = orderColorTests.iterator(); iterator.hasNext();) {
			oct = (OrderColorTest) iterator.next();
			break;
		}
		if(oct != null){
			writeFile(realFileDir + oct.getPhoto1Name(), oct.getPhoto1());
			if(oct.getPhoto1Name() != null){
				rtn = oct.getPhoto1Name();
			}
		}else{
			if(color.getBiDZColor() != null){
				rtn = getIsColorApprovedPhoto(color.getBiDZColor());
			}
		}
		return rtn;
	}
}
